/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datasource;

import java.io.Serializable;
import java.util.Random;

/**
 *
 * @author minaev
 */
public class AirshipKitchenSource implements DataSource, Serializable {

    double buf[];
    int cur_buf = 0;
    int cur_depth = 0;
    int final_depth_gelium;
    Random r = new Random();

    public AirshipKitchenSource(int final_depth_gelium) {
        this.final_depth_gelium = final_depth_gelium;
        buf = new double[final_depth_gelium];
    }

    
    
    @Override
    public double getData(double param, double param2) {
        int depth_gelium = 0;
        double res = 0;
        buf[cur_buf] = param;
        
        if(cur_depth<final_depth_gelium){
            cur_depth++;
        }
        if(++cur_buf>=buf.length){
            cur_buf = 0;
        }
        
        while (depth_gelium < cur_depth) {
            res += buf[depth_gelium];
            depth_gelium++;
        }
        
        res /= (depth_gelium);
        res += (1 - Math.exp(-Math.abs(buf[cur_buf-1>0?cur_buf-1:final_depth_gelium-1] - 
                buf[cur_depth == final_depth_gelium?cur_buf:0] ))) * 2 
                * Math.signum(buf[cur_buf-1>0?cur_buf-1:final_depth_gelium-1] - 
                buf[cur_depth == final_depth_gelium?cur_buf:0] );
        res += 2 * (r.nextDouble() - 0.5) + param2 * 0.2;
        return res;
    }
}
